package com.zmn.mcc.services.impl.sys;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.mcc.model.entity.sys.McSys;
import com.zmn.mcc.model.entity.sys.McSysQuery;
import com.zmn.mcc.persistence.interfaces.sys.SysDao;
import com.zmn.mcc.services.interfaces.sys.SysService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/14 13:52
 */
@Service
public class SysServiceImpl implements SysService {

    @Resource
    private SysDao sysDao;

    @Override
    public McSys findByKey(Integer sysId) {
        return sysDao.findByKey(sysId);
    }

    @Override
    public List<McSys> listPageByQuery(McSysQuery query) {
        Integer count = this.countByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : sysDao.listPageByQuery(query);
    }

    @Override
    public List<McSys> listAll() {
        return this.listAll(GlobalDict.STATUS_ENABLE);
    }

    @Override
    public List<McSys> listAll(Integer status) {
        McSysQuery query = new McSysQuery();
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McSys> listByQuery(McSysQuery query) {
        return sysDao.listByQuery(query);
    }

    @Override
    public Integer countByQuery(McSysQuery query) {
        return sysDao.countByQuery(query);
    }


    @Override
    public Integer updateByKey(McSys sys) {
        if (sys.getUpdateTime() == null) {
            sys.setUpdateTime(DateUtil.getNow());
        }
        return sysDao.updateByKey(sys);
    }

    @Override
    public Integer updateStatus(Integer sysId, Integer status, String updater) {
        McSys sys = new McSys();
        sys.setSysId(sysId);
        sys.setStatus(status);
        sys.setUpdater(updater);
        return this.updateStatus(sys);
    }

    @Override
    public Integer updateStatus(McSys sys) {
        if (sys.getUpdateTime() == null) {
            sys.setUpdateTime(DateUtil.getNow());
        }
        return sysDao.updateByKey(sys);
    }

    @Override
    public Integer updateStatusBatch(List<Integer> sysIds, Integer status, String updater) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(sysIds);
        dto.setStatus(status.intValue());
        dto.setUpdater(updater);
        return updateStatusBatch(dto);
    }

    @Override
    public Integer updateStatusBatch(StatusDTO dto) {
        if (dto.getUpdateTime() == null) {
            dto.setUpdateTime(DateUtil.getNow());
        }
        return sysDao.updateStatusBatch(dto);
    }

    @Override
    public Integer updateSort(Integer sysId, Integer sort) {
        McSys sys = new McSys();
        sys.setSysId(sysId);
        sys.setSort(sort);
        return this.updateSort(sys);
    }

    @Override
    public Integer updateSort(McSys sys) {
        return sysDao.updateSort(sys);
    }


    @Override
    public Integer insert(McSys sys) {
        sys.setSort(100);
        sys.setStatus(GlobalDict.STATUS_ENABLE);
        if (sys.getUpdateTime() == null) {
            sys.setUpdateTime(DateUtil.getNow());
        }
        sys.setCreater(sys.getUpdater());
        sys.setCreateTime(sys.getUpdateTime());
        return sysDao.insert(sys);
    }


    @Override
    public Integer deleteByKey(Integer sysId) {
        return sysDao.deleteByKey(sysId);
    }
}
